<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="产品代码" prop="productCode">
        <el-input v-model="queryParams.productCode" placeholder="请输入产品代码" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="信用评级" prop="creditRating">
        <!-- <el-input v-model="queryParams.productType" placeholder="请输入产品等级" clearable size="small"
          @keyup.enter.native="handleQuery" /> -->
          <el-select @keyup.enter.native="handleQuery" clearable class="width100" v-model="queryParams.creditRating" placeholder="请选择信用评级">
            <el-option v-for="dict in productTypeList" :key="dict.dictValue" :label="dict.dictValue"
              :value="dict.dictValue"></el-option>
          </el-select>
      </el-form-item>
<!--      <el-form-item label="期数" prop="periodNum">-->
<!--        <el-input v-model="queryParams.periodNum" placeholder="请输入期数" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
      <!-- <el-form-item label="信用评级" prop="creditRating">
        <el-input v-model="queryParams.creditRating" placeholder="请输入信用评级" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
<!--      <el-form-item label="单位" prop="unit">-->
<!--        <el-input v-model="queryParams.unit" placeholder="请输入单位" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际利率" prop="interestRate">-->
<!--        <el-input v-model="queryParams.interestRate" placeholder="请输入实际利率" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际服务费率" prop="serviceFee">-->
<!--        <el-input v-model="queryParams.serviceFee" placeholder="请输入实际服务费率" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="趸交" prop="isSinglePayment">-->
<!--        <el-input v-model="queryParams.isSinglePayment"  placeholder="请输入趸交" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="授信比例" prop="creditScale">-->
<!--        <el-input v-model="queryParams.creditScale" placeholder="请输入授信比例" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最低贷款额度" prop="minQuota">-->
<!--        <el-input v-model="queryParams.minQuota" placeholder="请输入最低贷款额度" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最高贷款额度" prop="maxQuota">-->
<!--        <el-input v-model="queryParams.maxQuota" placeholder="请输入最高贷款额度" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
      <el-form-item label="生效起始日期" prop="startDate">
        <el-date-picker clearable size="small" v-model="queryParams.startDate" type="date" value-format="yyyy-MM-dd"
          placeholder="选择生效起始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="生效结束日期" prop="endDate">
        <el-date-picker clearable size="small" v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd"
          placeholder="选择生效结束日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="滞纳金日费率" prop="dateLateFee">-->
<!--        <el-input v-model="queryParams.dateLateFee" placeholder="请输入滞纳金日费率" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="滞纳金按日收费宽限期" prop="lateFeeGracePeriod">-->
<!--        <el-input v-model="queryParams.lateFeeGracePeriod" placeholder="请输入滞纳金按日收费宽限期" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="滞纳金计算最大天数" prop="lateFeeMaxDays">-->
<!--        <el-input v-model="queryParams.lateFeeMaxDays" placeholder="请输入滞纳金计算最大天数" clearable size="small"-->
<!--          @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否默认产品" prop="defult">
       <!-- <el-input v-model="queryParams.defult" placeholder="请输入是否默认产品 Y是；N否" clearable size="small"
          @keyup.enter.native="handleQuery" /> -->
          <el-select class="width100" @keyup.enter.native="handleQuery" v-model="queryParams.defult" placeholder="请输入是否默认产品">
            <el-option v-for="dict in defultOptions" :key="dict.key" :label="dict.value"
              :value="dict.dictValue"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:ProductInfo:submit']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:ProductInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:ProductInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:ProductInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProductInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="产品代码" align="center" prop="productCode" width="150"/>
      <el-table-column label="产品名称" align="center" prop="productName" width="150"/>
      <el-table-column label="产品等级" align="center" prop="creditRating" />
      <el-table-column label="期数" align="center" prop="periodNum" />
      <!-- <el-table-column label="信用评级" align="center" prop="creditRating" /> -->
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="展示利率" align="center" prop="showInterestRate" />
      <el-table-column label="实际利率" align="center" prop="interestRate" />
      <el-table-column label="展示服务费率" align="center" prop="showServiceRate" />
      <el-table-column label="实际服务费率" align="center" prop="serviceFee" />
      <el-table-column label="趸交" align="center" prop="isSinglePayment" />
      <el-table-column label="授信比例" align="center" prop="creditScale" />
      <el-table-column label="最低贷款额度" align="center" prop="minQuota" />
      <el-table-column label="最高贷款额度" align="center" prop="maxQuota" />
      <el-table-column label="生效起始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生效结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="滞纳金日费率" align="center" prop="dateLateFee" />
      <el-table-column label="滞纳金按日收费宽限期" align="center" prop="lateFeeGracePeriod" />
      <el-table-column label="滞纳金计算最大天数" align="center" prop="lateFeeMaxDays" />
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" />
      <el-table-column label="是否默认产品" align="center" prop="defult" :formatter="defultFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:ProductInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:ProductInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="138px">
        <el-form-item label="产品代码" prop="productCode">
          <el-input :disabled="inputStatus" v-model="form.productCode" placeholder="请输入产品代码" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input :disabled="inputStatus" v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品等级" prop="creditRating">
          <!-- <el-input v-model="form.productType" placeholder="请输入产品等级" /> -->
          <el-select class="width100" clearable  v-model="form.creditRating" placeholder="请选择产品等级">
            <el-option v-for="dict in productTypeList" :key="dict.dictValue" :label="dict.dictValue"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="期数" prop="periodNum">
          <el-input :disabled="inputStatus" v-model="form.periodNum" placeholder="请输入期数" />
        </el-form-item>
      <!--  <el-form-item label="信用评级" prop="creditRating">
          <el-input :disabled="inputStatus" v-model="form.creditRating" placeholder="请输入信用评级" />
        </el-form-item> -->
        <el-form-item label="单位" prop="unit">
          <el-input :disabled="inputStatus" v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="展示利率" prop="showInterestRate">
          <el-input :disabled="inputStatus" v-model="form.showInterestRate" placeholder="请输入展示利率" />
        </el-form-item>
        <el-form-item label="实际利率" prop="interestRate">
          <el-input :disabled="inputStatus" v-model="form.interestRate" placeholder="请输入利(息)率" />
        </el-form-item>
        <el-form-item label="展示服务费率" prop="showServiceRate">
          <el-input :disabled="inputStatus" v-model="form.showServiceRate" placeholder="请输入服务费率" />
        </el-form-item>
        <el-form-item label="实际服务费率" prop="serviceFee">
          <el-input :disabled="inputStatus" v-model="form.serviceFee" placeholder="请输入服务费率" />
        </el-form-item>
        <el-form-item label="趸交" prop="isSinglePayment">
          <el-input :disabled="inputStatus" maxlength="1" v-model="form.isSinglePayment" placeholder="请输入趸交" />
        </el-form-item>
        <el-form-item label="授信比例" prop="creditScale">
          <el-input :disabled="inputStatus" v-model="form.creditScale" placeholder="请输入授信比例" />
        </el-form-item>
        <el-form-item label="最低贷款额度" prop="minQuota">
          <el-input :disabled="inputStatus" v-model="form.minQuota" placeholder="请输入最低贷款额度" />
        </el-form-item>
        <el-form-item label="最高贷款额度" prop="maxQuota">
          <el-input :disabled="inputStatus" v-model="form.maxQuota" placeholder="请输入最高贷款额度" />
        </el-form-item>
        <el-form-item label="生效起始日期" prop="startDate">
          <el-date-picker  class="width100" :disabled="inputStatus" clearable size="small" v-model="form.startDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择生效起始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生效结束日期" prop="endDate">
          <el-date-picker class="width100" :disabled="inputStatus" clearable size="small" v-model="form.endDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择生效结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="滞纳金日费率" prop="dateLateFee">
          <el-input :disabled="inputStatus" v-model="form.dateLateFee" placeholder="请输入滞纳金日费率" />
        </el-form-item>
        <el-form-item label="滞纳金日收费宽限期" prop="lateFeeGracePeriod">
          <el-input :disabled="inputStatus" v-model="form.lateFeeGracePeriod" placeholder="请输入滞纳金按日收费宽限期" />
        </el-form-item>
        <el-form-item label="滞纳金计算最大天数" prop="lateFeeMaxDays">
          <el-input :disabled="inputStatus" v-model="form.lateFeeMaxDays" placeholder="请输入滞纳金计算最大天数" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select class="width100" :disabled="inputStatus" v-model="form.state" placeholder="请选择状态">
            <el-option v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否默认产品" prop="defult">
          <el-select class="width100" :disabled="inputStatus" v-model="form.defult" placeholder="请输入是否默认产品">
            <el-option v-for="dict in defultOptions" :key="dict.key" :label="dict.value"
              :value="dict.key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否支持延期还款" prop="isDeferred">
          <el-select class="width100" :disabled="inputStatus" v-model="form.isDeferred" placeholder="请选择是否支持延期还款">
            <el-option v-for="dict in isDeferredOptions" :key="dict.key" :label="dict.value"
              :value="dict.key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="延期还款产品" prop="deferredId" v-if="form.isDeferred == '1'">
          <el-select class="width100" :disabled="inputStatus" multiple v-model="deferredIdList" placeholder="请选择延期还款产品" @change="deferredIdChange">
            <el-option v-for="dict in deferredOptions" :key="dict.id" :label="dict.productName +'('+ dict.creditRating+')'"
              :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="延期最大天数" prop="deferredMaxDays">
          <el-input type="number" min='0' :disabled="inputStatus" v-model="form.deferredMaxDays" placeholder="请输入延期最大天数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listProductInfo,
    getProductInfo,
    delProductInfo,
    addProductInfo,
    updateProductInfo,
    exportProductInfo,
    getFinancial
  } from "@/api/business/ProductInfo";
  import { listData, getData, delData, addData, updateData, exportData} from "@/api/system/dict/data";

  export default {
    name: "ProductInfo",
    components: {},
    data() {
      return {
        deferredIdList:'',
        deferredOptions:[],
        productTypeList:{},
        defultOptions:[
        {value:'是',key:'Y'},
        {value:'否',key:'N'}
        ],
        isDeferredOptions:[
        {value:'是',key:1},
        {value:'否',key:0}
        ],
        inputStatus:false,//输入框的禁用状态
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 产品信息表格数据
        ProductInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        stateOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productCode: null,
          productName: null,
          productType: null,
          periodNum: null,
          creditRating: null,
          unit: null,
          interestRate: null,
          serviceFee: null,
          isSinglePayment: null,
          creditScale: null,
          minQuota: null,
          maxQuota: null,
          startDate: null,
          endDate: null,
          dateLateFee: null,
          lateFeeGracePeriod: null,
          lateFeeMaxDays: null,
          state: null,
          defult: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          createTime: [{
            required: true,
            message: "创建时间不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_common_status").then(response => {
        this.stateOptions = response.data;
      });
      this.listDataFun()
      // 延期还款产品list
      this.getFinancialFun()
    },
    methods: {
      deferredIdChange(){
        var main = this
        let arr1 = ''
        arr1= this.deferredIdList.join(',')
        this.form.deferredId = arr1
      },
      listDataFun(){
        var main = this
        listData({
          pageSize:50,
          dictType:'product_type'
        }).then(res => {
          main.productTypeList = res.rows
        });
      },
      getFinancialFun(){
        var main = this
        getFinancial({}).then(res => {
          main.deferredOptions = res
        });
      },
      defultFormat(res){
        console.log(res,'resresres')
        switch (res.defult){
          case 'Y':
          return '是'
            break;
          case 'N':
          return '否'
            break;
          default:
            break;
        }
      },
      /** 查询产品信息列表 */
      getList() {
        this.loading = true;
        listProductInfo(this.queryParams).then(response => {
          this.ProductInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          productCode: null,
          productName: null,
          productType: null,
          periodNum: null,
          creditRating: null,
          unit: null,
          interestRate: null,
          serviceFee: null,
          isSinglePayment: null,
          creditScale: null,
          minQuota: null,
          maxQuota: null,
          startDate: null,
          endDate: null,
          dateLateFee: null,
          lateFeeGracePeriod: null,
          lateFeeMaxDays: null,
          state: null,
          createTime: null,
          defult: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.inputStatus = false
        this.title = "添加产品信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getProductInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          // this.inputStatus = true
          this.title = "修改产品信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateProductInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProductInfo(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).catch(res => {
                // this.msgError("新增失败")
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除产品信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有产品信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProductInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style type="text/css" scoped>
  .width100{
    width:100%
  }
  .width100 .el-date-editor.el-input{
    width:100%
  }
</style>
